<template>
    <nav class="navbar">
        <div class="menu-toggle" @click="toggleMenu">☰菜单导航</div>
        <ul class="nav-links" :class="{ 'active': menuVisible }">
            <li><router-link to="/" class="nav-link" @click="handleLinkClick">首页</router-link></li>
            <li><router-link to="/mapSystem" class="nav-link" @click="handleLinkClick">区域综合</router-link></li>
            <li><router-link to="/todoList" class="nav-link" @click="handleLinkClick">我的待办</router-link></li>
            <li><router-link to="/mall" class="nav-link" @click="handleLinkClick">去逛逛</router-link></li>
            <li><router-link to="/news" class="nav-link" @click="handleLinkClick">及时资讯</router-link></li>
            <li><router-link to="/store" class="nav-link" @click="handleLinkClick">我的商店</router-link></li>
            <li><router-link to="/personal" class="nav-link" @click="handleLinkClick">个人中心</router-link></li>
        </ul>
        <div class="login-container">
            <template v-if="isLoggedIn">
                <div class="icon-container">
                    <router-link to="/message" class="icon-button">
                        <img :src="msgSvg" alt="消息通知图标" width="24" height="24" />
                    </router-link>
                </div>
                <div class="user-profile" @click="toggleUserMenu">
                    <img :src="userAvatar" alt="用户头像" class="avatar" />
                    <span class="username">{{ username }}</span>
                    <div :class="['dropdown-menu', { visible: menuUserVisible }]">
                        <router-link to="/personal" class="menu-item">个人中心</router-link>
                        <a @click="logout" class="menu-item">注销</a>
                    </div>
                </div>
            </template>
            <template v-else>
                <router-link to="/login" class="login-button">登录</router-link>
            </template>
        </div>
    </nav>
</template>

<script setup>
import { useTokenStore } from '@/stores/token';
import { onMounted, onUnmounted, computed, ref } from 'vue';
import { getUserInfo } from '@/api/user'
import msgSvg from '@/assets/svg/msg.svg'
const tokenStore = useTokenStore();
const isLoggedIn = ref(!!localStorage.getItem('token')); // 使用 ref 替代 computed
const username = computed(() => tokenStore.username);
const userAvatar = ref()

const menuUserVisible = ref(false);
const menuVisible = ref(false);
const toggleUserMenu = () => {
    menuUserVisible.value = !menuUserVisible.value;
};

const toggleMenu = () => {
    menuVisible.value = !menuVisible.value; // 切换菜单的可见性
};
const handleLinkClick = () => {
    menuVisible.value = false; // 点击链接后隐藏菜单
};

const handleOutsideClick = (event) => {
    if (!event.target.closest('.user-profile')) {
        menuUserVisible.value = false;
    }
};

const logout = () => {
    tokenStore.clearToken();
    localStorage.removeItem('token');
    isLoggedIn.value = false; // 由于使用了 ref，现在可以直接设置值
};

onMounted(() => {
    window.addEventListener('click', handleOutsideClick);
    console.log(username.value);
});

onUnmounted(() => {
    window.removeEventListener('click', handleOutsideClick);
});


const userInfo = ref({})
const getUserInfos = () => {
    getUserInfo().then(res => {
        userInfo.value = res.data
        userAvatar.value = res.data.avatar
    })
}


onMounted(() => {
    getUserInfos()
});

</script>

<style scoped>
.navbar {
    width: 100vw;
    background: #f2f4f5;
    color: black;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
    /* border-radius: 0 0 10px 10px; */
    position: sticky;
    /* 吸顶效果 */
    top: 0;
    /* 吸顶位置 */
    z-index: 1000;
    /* 确保在最上层 */
    /* box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); */
    font-family: 'Arial', sans-serif;
    /* 可以替换为你喜欢的字体 */
}

.menu-toggle {
    display: none;
    /* 默认隐藏菜单切换按钮 */
    cursor: pointer;
    font-size: 24px;
}


.nav-links {
    border: 1px solid transparent;
    /* 添加透明边框 */
    display: flex;
    list-style: none;
    /* 去除列表样式 */
    padding-left: 0;
    /* 去除左边距 */
}

.nav-link {
    color: black;
    font-family: 'Georgia', serif;
    text-decoration: none;
    margin: 0 10px;
    padding: 5px 15px;
    border-radius: 5px;
    transition: background 0.3s;
}

.nav-link:hover {
    background: rgba(55, 51, 51, 0.2);
    /* 悬停效果 */
    /* 添加下划线 */
}

/* 选中状态 */
.nav-link.router-link-exact-active {
    background: #714949;
    color: white;
}



.login-container {
    display: flex;
    /* 使用flex布局以便对齐 */
    align-items: center;
    /* 垂直居中对齐 */
    margin-left: auto;
}

.icon-container {
    display: flex;
    align-items: center;
    margin-right: 10px;
    /* 图标与头像之间的间距 */
}

.icon-button {

    /* 图标颜色 */
    margin-right: 10px;
    /* 图标与头像之间的间距 */
}

.icon-button {
    display: inline-block;
    /* 确保元素是块状的 */
    transition: fill 0.3s;
    /* 添加过渡效果 */
}




.user-profile {
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: pointer;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
    border: 2px solid white;
    /* 头像添加白色边框 */
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
    /* 头像添加阴影 */
}

.username {
    margin-right: 10px;
    font-size: 10px;
    color: black;
    font-weight: bold;
    /* 用户名加粗 */
}

.dropdown-menu {
    position: absolute;
    background: rgba(255, 255, 255, 0.9);
    /* 下拉菜单背景 */
    color: black;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    margin-top: 20px;
    z-index: 1000;
    transition: opacity 0.3s ease, transform 0.3s ease;
    opacity: 0;
    transform: translateY(-10px);
    pointer-events: none;
}

.dropdown-menu.visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.menu-item {
    display: block;
    padding: 10px 15px;
    text-decoration: none;
    color: black;
    border-radius: 5px;
    transition: background 0.3s;
}

.menu-item:hover {
    background: #714949;
    /* 悬停效果 */
}

.login-button {
    color: rgb(52, 51, 51);
    padding-right: 20px;
}

@media (max-width: 768px) {

    .menu-toggle {
        display: block;
        /* 在手机上显示菜单切换按钮 */
    }


    .navbar {
        width: 100vw;
        background: #f2f4f5;
        color: black;
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        flex-wrap: nowrap;
        /* border-radius: 0 0 10px 10px; */
        /* position: sticky; */
        /* 吸顶效果 */
        top: 0;
        /* 吸顶位置 */
        z-index: 1000;
        /* 确保在最上层 */
        /* box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); */
        font-family: 'Arial', sans-serif;
        /* 可以替换为你喜欢的字体 */

    }



    .nav-links {
        display: none;
        /* 默认隐藏导航链接 */
        flex-direction: column;
        /* 垂直排列 */
        width: 100%;
    }

    .nav-links.active {
        display: flex;
        /* 当菜单被激活时显示 */
    }

    .nav-link {
        padding: 10px;
        /* 增加点击区域 */
        width: 100%;
        /* 全宽 */
        margin: 0 10px;

        text-align: left;
        /* 左对齐 */
    }

    .avatar {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        margin-right: 10px;
        border: 2px solid white;
        /* 头像添加白色边框 */
        box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
        /* 头像添加阴影 */
    }

    .username {
        /* 隐藏 */
        display: none;
    }

    .login-container {
        display: flex;
        margin-top: 10px;
        text-align: center;
    }

    .user-profile {
        display: flex;
        flex-direction: column;
        align-items: center;
        cursor: pointer;
    }
}
</style>